/////////////////////////////////////////////////////////////////////////////////////////////////////////
* This do file generates Table A.3; Table 5; Table 7; Table A.5; 
* Table A.7; Table A.4; Table A.6; Table 6; & Fig 2
/////////////////////////////////////////////////////////////////////////////////////////////////////////

use "${inputdir}/prospects_analysis_dataset_clean.dta", clear

/////////////////////////////////////////////////////////////////////////////////////////////		
*								 Prep and cleanup 											*
/////////////////////////////////////////////////////////////////////////////////////////////	
	drop if interviews!=2	//     Respondents without endline	

	foreach v in working hrsworked_trim lhrsworked hrsworked_ihst inc7d_raw inc7d_trim linc7d inc7d_ihst inc90d_trim linc90d inc90d_ihst lfi lfix lfix_new{
		gen `v'_L1 = `v'1
		}	
	
	drop working				
	
	foreach v in welfare_today selfesteem locuscontrol aggression riskybehavior{
		replace r1_`v' = r1_`v'_std				// use standardized version of variables.
		replace r2_`v' = r2_`v'_std
		rename r1_`v' `v'1						// rename for reshape
		rename r2_`v' `v'2
		rename r1_`v'_std `v'_std1
		rename r2_`v'_std `v'_std2		
		gen `v'_L1 = `v'1
		}
		
		gen psi_L1 = psi_std1
		gen everattend = sfc_totalAttendance > 0
		replace everattend = . if SFC2!=1
		gen totattend = sfc_totalAttendance
		replace totattend = . if SFC2!=1	
		gen everattend_L1 = 0
		gen totattend_L1 = 0 	

	gen lei_L1 = LifeEvent_index1
	gen lei2 = LifeEvent_index2

*Reshape to Long format
	
	reshape long lei working hrsworked_trim lhrsworked hrsworked_ihst inc7d_trim linc7d inc7d_ihst lfi lfix lfix_new inc90d_trim linc90d inc90d_ihst welfare_today selfesteem locuscontrol aggression riskybehavior ///
		welfare_today_std selfesteem_std locuscontrol_std aggression_std riskybehavior_std psi_std, i(respid) j(time)

	gen psi = psi_std		
	replace sfc_any = 0 if time==1
	cap drop SFC
	gen SFC = (time==2) * ANYSFC
	bys respid: egen anysfc = max(sfc_any)
	
	cap drop  *goodtot  *_good 
	cap drop SFC2
	bys respid: egen SFC2 = max(SFC)
	
	* Predicted Outcomes for main indices
		foreach oc in lfi psi{
				reg `oc' female reg_age age2 i.r1_HighGrade notrain i.commid if SFC==0 & time==2
				predict `oc'_hat, xb
				gen `oc'_badhat = -1 * `oc'_hat							// flip sign to predict disadvantage
				sum `oc'_badhat, detail
				gen `oc'_hat_low = `oc'_badhat > r(p50)
				replace `oc'_hat_low = . if `oc'_badhat ==.
				gen SFCx`oc'_hat_low = `oc'_hat_low * SFC
			}
		
		gen anyone = 1
		replace anyone = . if time==2
		bys sfcgroupid: egen grpsize = total(anyone)
		
		gen temp = lfi
		replace temp = . if time==2
		bys respid: egen templfi_L1 = max(temp)
		drop temp
		
		gen temp = psi
		replace temp = . if time==2
		bys respid: egen temppsi_L1 = max(temp)
		drop temp		
		
		gen inboth_lfi = 1
		replace inboth_lfi = 0 if lfi_hat==. | lfi==. | templfi_L1==.	
		
	tempfile prepdata
	save `prepdata'	

/////////////////////////////////////////////////////////////////////////////////////////////		
*								 TABLE A.3												*
/////////////////////////////////////////////////////////////////////////////////////////////	


local controls1 = "female reg_age age2 i.r1_HighGrade "
replace lfi_L1 = lfi1anc									// use ancova baseline
gen Y_L1 = .
	
gen SFCxany = SFC * sfc_any
gen SFCxtotdays = SFC * totattend

replace lfix_new_L1 = lfix_new1anc						
gen SFCxLEI = SFC * lei
gen SFCxhighherf = SFC * highherf 
gen SFCxherfsd = SFC * herf_sd

gen all=1
replace psi = psi_std

replace everattend = 0 if time==1
replace everattend = 0 if SFC==0
replace totattend = 0 if time==1
replace totattend = 0 if SFC==0

foreach v in selfesteem locuscontrol aggression riskybehavior psi{
	replace `v' = . if welfare_today==.
	}

* FIRST STAGE: APPENDIX TABLE A3

	reghdfe everattend SFC `controls1'  if time==2 , absorb(commid) vce(cluster respid)
	matrix table = r(table)
	estadd scalar t = round(table[3,1], 0.001)
	sum everattend if time==2
	estadd scalar ymean = r(mean)	
	sum everattend if SFC==0 & time==2
	estadd scalar cm = r(mean)
	eststo r1
	
	reghdfe totattend SFC `controls1'  if time==2 , absorb(commid) vce(cluster respid)
	matrix table = r(table)
	estadd scalar t = round(table[3,1], 0.001)	
	sum totattend if time==2
	estadd scalar ymean = r(mean)	
	sum totattend if SFC==0 & time==2
	estadd scalar cm = r(mean)
	eststo r2	
	
		# delimit ;
			esttab r1 r2 using "${outputdir}/tables/Table_A3.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "t T-Stat for SFC"  "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f  %10.3f  %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC* female *age*) 
			order( ) mlabels("Ever Attend" "\# Sessions Attended") nogaps varlabels(female "Female" reg_age "Age" age2 "Age-Squared")
			align(cccccccc) title(SFC Assignment and SFC Attendance \label{firststage}) note(Column (1) is the first stage of treatment on the treated estimation.) nomtitles;
		# delimit cr		
*/		
/////////////////////////////////////////////////////////////////////////////////////////////		
*					Table 5; Table 7; Table A.5; TABLE A7; TABLE A4; Table A6												*
/////////////////////////////////////////////////////////////////////////////////////////////	

local t = 1		
foreach oc in everattend totattend psi lfi  hrsworked_trim inc7d_ihst welfare_today selfesteem locuscontrol aggression riskybehavior inc90d_ihst lfix_new{	
	
		replace Y_L1 = `oc'_L1
		
		* Main core specification with no interactions
		reghdfe `oc' SFC Y_L1 `controls1'  if time==2 , absorb(commid) vce(cluster respid)
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test SFC=0
		estadd scalar pval = r(p)
		eststo anc_b`t'	
		
		* Result heterogeneity by CFW interaction - APPENDIX TABLE A6
		reghdfe `oc' SFC BOTH Y_L1 `controls1'  if time==2 , absorb(commid) vce(cluster respid)
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test SFC=0
		estadd scalar pval = r(p)
		test SFC + BOTH = 0
		estadd scalar p = r(p)
		eststo a`t'		
		
		* Herfindahl for TABLE 7 PANEL B	
		reghdfe `oc' SFC SFCxherfsd Y_L1 `controls1'  if time==2 , absorb(commid) vce(cluster sfcgroupid)
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test SFC=0
		estadd scalar pval = r(p)
		test SFC + SFCxherfsd = 0
		estadd scalar p = r(p)
		eststo r`t'		
		
		* LIFE EVENT INTERACTION 
		reghdfe `oc' SFC SFCxLEI lei Y_L1 lei_L1 `controls1' if time==2 , absorb(commid) vce(cluster respid)
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test SFC=0
		estadd scalar pval = r(p)
		test SFCxLEI=0
		estadd scalar pval2 = r(p)
		estadd scalar p = .
		eststo h`t'				
		
		local t = `t' + 1
		}		
		
		replace Y_L1 = lfi_L1		
		reghdfe lfi Y_L1 `controls1'  (sfc_any = SFC) if time==2 , absorb(commid) vce(cluster respid) old
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test sfc_any=0
		estadd scalar pval = r(p)			
		eststo lfi_iv	
	
		replace Y_L1 = psi_L1		
		loc oc = "psi"					
		reghdfe psi Y_L1 `controls1' (sfc_any = SFC) if time==2, absorb(commid) vce(cluster respid) old
		estadd loc tfe = "Yes"
		estadd ysumm	
		sum `oc' if time==2 	
		estadd loc num = r(N)		
		sum `oc' if ANYSFC==0 & time==2 & inboth==1
		estadd scalar cm = r(mean)
		test sfc_any=0
		estadd scalar pval = r(p)			
		eststo psi_iv	
	
		* TABLE 5
		# delimit ;
			esttab anc_b3 anc_b4 psi_iv lfi_iv h3 h4 using "${outputdir}/tables/Table_5.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC* sfc_any lei *L1) 
			order(SFC sfc_any SFCxLEI lei lei_L1 Y_L1) mlabels(PSI LFI PSI LFI PSI LFI ) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(Impact of SFC - ANCOVA \label{mainresanc}) nonotes nomtitles;
		# delimit cr

		* TABLE 7 PANEL B
		# delimit ;
			esttab r1 r2 r3 r4 using "${outputdir}/tables/Table_7_panelb.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "p P-Val: B1+B2=0" "pval P-Val" "r2 R2")	sfmt(%10.4f %10.4f %10.4f  %10.4f %10.4f %10.4f %10.4f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC* *L1) 
			order(SFC BOTH SFC* Y_L1) mlabels(Any Tot PSI LFI) nogaps varlabels(SFCxherfsd "SFC x Herfindahl SD")
			align(cccccccc) title(Effects of Group Cohesion, Panel (b)) note(SE clustered at SFC group level) nomtitles;
		# delimit cr	
		
		* APPENDIX TABLE A5 PANEL A
		# delimit ;
			esttab anc_b5 anc_b6 anc_b4 using "${outputdir}/tables/Table_A5_panela.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  *L1) 
			order(SFC Y_L1) mlabels(Hours "IHST(7dI)" LFI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Labor Outcomes, and Resiliency, Panel(a) \label{mainresanc}) nonotes nomtitles;
		# delimit cr	
		
		* APPENDIX TABLE A5 PANEL B
		# delimit ;
			esttab h5 h6 h4 using "${outputdir}/tables/Table_A5_panelb.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  lei *L1) 
			order(SFC SFCxLEI lei lei_L1 Y_L1) mlabels(Hours "IHST(7dI)" LFI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Labor Outcomes, and Resiliency, Panel(b) \label{mainresanc}) nonotes nomtitles;
		# delimit cr	
			
		* APPENDIX TABLE A7 PANEL A
		# delimit ;
			esttab anc_b5 anc_b6 anc_b12 anc_b13 using "${outputdir}/tables/Table_A7_panela.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  *L1) 
			order(SFC Y_L1) mlabels(Hours "IHST(7dI)" "IHST(3mI)"  LFI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Financial Outcomes, and Resiliency with three month income, Panel(a)  w/ 90d Inc) nonotes nomtitles;
		# delimit cr	
		
		* APPENDIX TABLE A7 PANEL B
		# delimit ;
			esttab h5 h6 h12 h13 using "${outputdir}/tables/Table_A7_panelb.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  lei *L1) 
			order(SFC SFCxLEI lei lei_L1 Y_L1) mlabels(Hours "IHST(7dI)" "IHST(3mI)" LFI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Financial Outcomes, and Resiliency with three month income, Panel(b)   w/ 90d Inc) nonotes nomtitles;
		# delimit cr	

		* APPENDIX TABLE A4 PANEL A
		# delimit ;
			esttab anc_b7 anc_b8 anc_b9 anc_b10 anc_b11 anc_b3 using "${outputdir}/tables/Table_A4_panela.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  *L1) 
			order(SFC Y_L1) mlabels(WF SE LOC Agro RB PSI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Psychosocial Outcomes, and Resiliency, Panel(a) \label{mainresanc}) nonotes nomtitles;
		# delimit cr	
		
		* APPENDIX TABLE A4 PANEL B
		# delimit ;
			esttab h7 h8 h9 h10 h11 h3 using "${outputdir}/tables/Table_A4_panelb.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")	sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC*  lei *L1) 
			order(SFC  SFCxLEI lei lei_L1 Y_L1) mlabels(WF SE LOC Agro RB PSI) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
			align(cccccc) title(SFC, Psychosocial Outcomes, and Resiliency, Panel(b) \label{mainresanc}) nonotes nomtitles;
		# delimit cr
		
		* APPENDIX TABLE A6
		# delimit ;
              esttab a3 a4  using "${outputdir}/tables/Table_A6.tex", replace b(%10.3f) se(%10.3f) scalars(
              "ymean Mean Y" "cm Control Mean" "pval P-Val" "pval2 P-Val 2" "r2 R2")   sfmt(%10.3f %10.3f %10.3f %10.3f %10.3f %10.3f) star(* 0.10 ** 0.05 *** 0.01) keep(SFC BOTH Y_L1 ) 
              order(SFC BOTH Y_L1) mlabels(PSI LFI ) nogaps varlabels(sfc_any "Attended SFC" lei "Life Event Index (LEI)" lei_L1 "LEI_L1")
              align(cc) title(Robustness of SFC Impacts to CFW Interactions) nonotes nomtitles;
		# delimit cr         

	estimates clear		
	*/

////////////////////////////////////////////////////////////////////////////////////////////////
* 									FIGURE 2
////////////////////////////////////////////////////////////////////////////////////////////////

local controls1 = "female reg_age age2 i.r1_HighGrade notrain"

* Set up treatments
	
	sum age, detail
	gen young = age <= r(p50)
	replace young = . if age==.
	gen lowed = r1_HighGrade<12
	replace lowed=. if r1_HighGrade ==16 							
	
	foreach het in young lowed notraining female{ 				
		gen SFCx`het' = SFC * `het'
		}
		
	gen SFCxHet = .
	gen Het = . 	
	
	*
	foreach v in welfare_today selfesteem locuscontrol aggression riskybehavior{
		gen temp = `v'
		replace temp = . if time==2
		bys respid: egen temp2 = max(temp)
		cap gen `v'_L1 = temp2
		drop temp*
		}

* BAR PLOT
	cap drop SFC2
	local h = 1	
	local t = 1
	foreach het in female young lowed notraining lfi_hat_low{
						
			gen SFC`h' = SFC			
						
			reghdfe lfi SFC`h' lfi_L1 `controls1' if time==2 & `het'==1, a(commid) vce(cluster respid)				
			eststo r`h'
			drop SFC`h'

			local h = `h' + 1
			gen SFC`h' = SFC			
			
			reghdfe lfi SFC`h' lfi_L1 `controls1'  if time==2 & `het'==0, a(commid) vce(cluster respid)				
			eststo r`h'	
			drop SFC`h'
			
			local h = `h' + 1

			local t = `t' + 1
		}

	* FIGURE 2
	coefplot (r1, mcolor(gs0) ciopts(color(gs0)) msymbol(circle)) ///
			 (r2, mcolor(gs0) ciopts(color(gs0) lpattern(dash)) msymbol(diamond)) ///
			 (r3, mcolor(gs0) ciopts(color(gs0)) msymbol(circle)) ///
			 (r4, mcolor(gs0) ciopts(color(gs0) lpattern(dash))  msymbol(diamond)) ///
			 (r5, mcolor(gs0) ciopts(color(gs0))  msymbol(circle)) ///
			 (r6, mcolor(gs0) ciopts(color(gs0) lpattern(dash)) msymbol(diamond)) /// 
			 (r7, mcolor(gs0) ciopts(color(gs0)) msymbol(circle)) ///
			 (r8, mcolor(gs0) ciopts(color(gs0) lpattern(dash)) msymbol(diamond)) ///
			 (r9, mcolor(gs0) ciopts(color(gs0))  msymbol(circle)) ///
			 (r10, mcolor(gs0) ciopts(color(gs0) lpattern(dash)) msymbol(diamond)),  ///
		legend(off) graphregion(color(white)) drop(*L1 _cons) keep(*SFC*) xline(0) nolabels  ///
		ylabel(, angle(0)) note() title() nooffset xlabel(, format(%02.1f)) xtitle("Estimated SFC Impact", height(7)) ///
		groups(SFC1 SFC2 ="Gender" SFC3 SFC4 ="Age" SFC5 SFC6="Educ" SFC7 SFC8="Training" SFC9 SFC10="Pred LFI") ///
		coeflabels(SFC1 = "Female" SFC2 = "Male" SFC3 ="<=21"  SFC4= ">21" SFC5="<=10 Yrs" SFC6=">10 Yrs" SFC7="None" SFC8="Any"  SFC9="Low" SFC10= "High") 
		graph export "${outputdir}/figures/Figure_2.pdf", replace		


////////////////////////////////////////////////////////////////////////////////////////////////
* 									TABLE 6
////////////////////////////////////////////////////////////////////////////////////////////////

cap drop Y_L1
gen Y_L1 = .

local a = 1
local b = 2
local c = 3
local d = 4

matrix results = [`a', `b' \ `c', `d']
matrix list results
		
	local o = 1
	foreach oc in lfi psi{	
	
	matrix results = [0, 0, 0, 0, 0, 0, 0]
	
		local h = 1
		foreach het in female young lowed notraining lfi_hat_low psi_hat_low {
		
		di "`oc' `het'"
		
		replace Het = `het'
		
		preserve			
			replace Y_L1 = `oc'_L1
			replace SFCxHet = SFCx`het'
			
			bys respid: egen inboth = count(`oc')
			replace inboth = inboth==2	
			
			reghdfe `oc' SFC SFCxHet Het Y_L1 `controls1'  if time==2, a(commid) vce(cluster respid)	
			matrix table = r(table)
			loc a1 = round(table[1,1], 0.001)
			loc a2 = round(table[1,2], 0.001)
			loc a3 = round(table[1,3], 0.001)
			loc a4 = round(table[1,4], 0.001)			
			loc b1 = round(table[2,1], 0.001)
			loc b2 = round(table[2,2], 0.001)
			loc b3 = round(table[2,3], 0.001)
			loc b4 = round(table[2,4], 0.001)	
			sum `oc' if ANYSFC==0 & time==2 & inboth==1 & `het'==1
			loc a5 = round(r(mean), 0.001)		
			sum `oc' if ANYSFC==0 & time==2 & inboth==1 & `het'==0
			loc a6 = round(r(mean), 0.001)	
			test SFC + SFCxHet = 0
			loc pt = round(r(p), 0.001)
			
		restore
		
		matrix results = [results \ `a1', `a2', `a3', `a4', `pt' , `a5', `a6' \ `b1', `b2', `b3', `b4', . , . , .]
		
		local h = `h' + 1		
		}
						
		local g = ((`h' - 1) * 2) + 1	
			
		matrix results = results[2..`g', .]			
			
		
	matrix rownames results = Female "\hspace{.2in}" Young "\hspace{.2in}" "Low Ed" "\hspace{.2in}" "No Training" "\hspace{.2in}"  /// 
		"Predicted Low LFI (0/1)" "\hspace{.2in}"  "Predicted Low PSI (0/1)" "\hspace{.2in}"    
	matrix colnames results = SFC "SFCxHet" "Het" Y_L1 "P: $\beta 1 + \beta 2$" "In Mean" "Out Mean"		

	esttab matrix(results) using "${outputdir}/tables/Table_6_panel`o'.tex", replace title(Heterogeneous Impacts on PSI and LFI - Panel `o') align(ccccccc)
		
		local o = `o' + 1	
	}
